#include <iostream>

using namespace std;

const int N = 1e6 + 10;

int n, h;
int ne[N];

int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> ne[i];
	cin >> h;
	
	for (int i = h; i != 0; i = ne[i])
		cout << i << " ";
	cout << "\n";
	
	return 0;
}
